package com.example.demo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.filterWapper.HttpServletRequestWrapper;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.*;

public class RenrenFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }


    //  registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/reg");


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest1 = (HttpServletRequest) servletRequest;
        if (!httpServletRequest1.getRequestURI().contains("/app/")) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            if (httpServletRequest1.getRequestURI().contains("/app/loginCheck/")) {
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                String token = null;
                HttpServletRequest hreq = (HttpServletRequest) servletRequest;
                ServletRequest requestWrapper = null;
                if (hreq.getMethod().equals("GET")) {
                    token = servletRequest.getParameter("token");
                    requestWrapper = servletRequest;
                } else {
                    requestWrapper = new BodyReaderHttpServletRequestWrapper(hreq);
                    String body = HttpHelper.getBodyString(requestWrapper);
                    if (body != null) {
                        JSONObject jt = JSONObject.parseObject(body);
                        token = jt.getString("token");
                    }
                }
                PrintWriter out = null;
                boolean result = true;
                if (token != null){
                    requestWrapper.setAttribute("we","SUCEESS");

                } else {
                    result = false;
                }
                if (!result) {
                    servletResponse.setCharacterEncoding("UTF-8");
                    servletResponse.setContentType("application/json; charset=utf-8");
                    JSONObject res = new JSONObject();
                    res.put("code", "800000");
                    res.put("message", "tokenʧЧ");
                    out = servletResponse.getWriter();
                    out.append(res.toString());
                    out.flush();
                } else {
                    filterChain.doFilter(requestWrapper, servletResponse);
                }
            }
        }

        PrintWriter out = null;
        boolean result = true;
        String token = null;
        ServletRequest request = null;
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        if (httpServletRequest.getMethod().equals("GET")) {
            token = servletRequest.getParameter("token");
            request = servletRequest;
        } else {
            HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(httpServletRequest);
            String params = requestWrapper.getRequestParams();
            JSONObject jt = JSONObject.parseObject(params);
            token = jt.getString("token");
            request=requestWrapper;
        }
        filterChain.doFilter(request, servletResponse);
    }

    @Override
    public void destroy() {

    }


}
